package com.wangyh.edit.prettyedit.mainframe;

import com.wangyh.edit.prettyedit.tree.LeftTreeView;
import com.wangyh.edit.prettyedit.pages.PageFactory;
import com.wangyh.edit.prettyedit.constant.EditConstant;
import com.wangyh.edit.prettyedit.topmenu.TopMenu;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.Window;

import java.io.IOException;

/**
 * 程序的主界面，包含菜单，左边文件树，中部编辑区
 * @author wangyh
 */
public class MainFrameView {

    private final Stage stage = new Stage();

    private BorderPane borderPane;

    /**
     * 构造stage，放入borderpan，展示界面
     */
    public void showView() {
        FXMLLoader fxmlLoader = new FXMLLoader(MainFrameView.class.getResource("/views/MainView.fxml"));

        // 1、初始化一个场景
        try {
            borderPane = (BorderPane)fxmlLoader.load();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Scene scene = new Scene(borderPane);
        // 样式文件最好在Scene中引入，这样其他子组件就都可以使用了（这里注意替换斜杠\,不能会报错找不到路径）
        scene.getStylesheets().add(this.getClass().getResource("/css/menu.css").toExternalForm());
        scene.getStylesheets().add(this.getClass().getResource("/css/tree.css").toExternalForm());
//        scene.getStylesheets().add(("css/menu.css").replace("\\", "/"));

        //将场景放入窗口
        stage.setScene(scene);

        //设置窗体样式
        configStage();

        //设置border的上下左右区域内容
        Platform.runLater(() -> {
            //把下面的预热一下
            Node about = PageFactory.createPageService("about").generatePage();
        });

        //空白欢迎界面
        Node text = PageFactory.createPageService("welcome").generatePage();
        borderPane.setCenter(text);

        //构造左侧树
        generateTree();

        //上方的菜单
        generateMenu();

        //底下的版权
        generateBottom();
    }

    /**
     * 设置窗口样式和属性
     */
    private void configStage(){
        Rectangle2D screenRectangle = Screen.getPrimary().getBounds();
        stage.setTitle(EditConstant.appName);
        //设置窗体标题小图标和任务栏图标
        stage.getIcons().add(new Image(getClass().getResourceAsStream("/img/Logo2.png")));
//        stage.setFullScreen(true);
        stage.setMaximized(true);
//        stage.setX(screenRectangle.getMinX());
//        stage.setY(screenRectangle.getMinY());
        stage.setWidth(screenRectangle.getWidth() * 0.8);
        stage.setHeight(screenRectangle.getHeight() * 0.85);
        // 3、打开窗口
        stage.show();
//        stage.centerOnScreen();//窗口居中，必须再show之后调用才能起效
    }

    /**
     * 构造上面的菜单
     */
    public void generateMenu(){
        MenuBar menuBar = TopMenu.getMenuBar(getClass(), MainFrameView.this);
        borderPane.setTop(menuBar);
    }

    /**
     * 构造左侧树
     */
    public void generateTree(){
        //左侧树
        LeftTreeView leftTreeView = new LeftTreeView();
        TreeView tree = leftTreeView.getTree();
//        tree.getStylesheets().add(MainFrameView.class.getResource("/css/menu.css").toExternalForm());

        borderPane.setLeft(tree);
    }

    /**
     * 构造底部版权
     */
    public void generateBottom(){
        Label copyRightLabel = new Label("CopyRight © 2022-2023 wangyonghu 版权所有 侵权必究");
        copyRightLabel.setFont(Font.font(12));
        borderPane.setBottom(copyRightLabel);
        BorderPane.setAlignment(copyRightLabel, Pos.CENTER);
        BorderPane.setMargin(copyRightLabel, new Insets(7,11,7,11));
    }

    public Window getWindow(){
        return borderPane.getScene().getWindow();
    }

    public BorderPane getSceneRoot(){
        return borderPane;
    }

    /**
     * 右侧页面路由
     */
    private Node routePage(Pane root, String itemName) {
        return PageFactory.createPageService(itemName).generatePageToRoot(root);
    }

}
